package models

import (
	"time"
)

type MediaUserInfo struct {
	ID               int       `gorm:"primary_key" json:"id"`
	MediaUserId      int       `json:"media_user_id"`
	Account          string    `json:"account"`
	Introduce        string    `json:"introduce"`
	Avatar           string    `json:"avatar"`
	OperatorsName    string    `json:"operators_name"`
	OperatorsIdNo    string    `json:"operators_id_no"`
	OperatorsIdPhoto string    `json:"operators_id_photo"`
	Address          int       `json:"address"`
	Email            string    `json:"email"`
	CreatedAt        time.Time `json:"created_at"`
	UpdatedAt        time.Time `json:"updated_at"`
}

//修改默认表名
func (MediaUserInfo) TableName() string {
	return "media_user_info"
}

// 增加日志
func MediaUserInfoAdd(data map[string]interface{}) error {

	mediaUserInfo := MediaUserInfo{
		MediaUserId:      data["media_user_id"].(int),
		Account:          data["account"].(string),
		Introduce:        data["introduce"].(string),
		Avatar:           data["avatar"].(string),
		OperatorsName:    data["operators_name"].(string),
		OperatorsIdNo:    data["operators_id_no"].(string),
		OperatorsIdPhoto: data["operators_id_photo"].(string),
		Address:          data["address"].(int),
		Email:            data["email"].(string),
		CreatedAt:        time.Now(),
		UpdatedAt:        time.Now(),
	}
	if err := DB.Create(&mediaUserInfo).Error; err != nil {
		return err
	}

	return nil
}

type Profile struct {
	Account       string `json:"account"`
	Introduce     string `json:"introduce"`
	Avatar        string `json:"avatar"`
	Identity      string `json:"identity"`
	ID            string `json:"id"`
	Address       string `json:"address"`
	WebSite       string `json:"web_site"`
	Phone         string `json:"phone"`
	OperatorsName string `json:"operators_name"`
	OperatorsIdNo string `json:"operators_id_no"`
	Email         string `json:"email"`
	IsExpire      bool   `json:"is_expire"`
}

func GetUserInfo(userId int) (*Profile, error) {
	var info Profile
	err := DB.Table("media_users u").Select("u.id,ui.account,ui.introduce,ui.avatar,u.identity,ui.address,uis.institution_web_site web_site,u.phone,ui.operators_name,ui.operators_id_no").Joins("left join media_user_info as ui on ui.media_user_id=u.id left join media_user_institution uis on uis.media_user_id=u.id").Where("u.id=?", userId).Scan(&info).Error
	if err != nil {
		return nil, err
	}
	return &info, err
}

type UserI struct {
	ID       int    `json:"id"`
	Avatar   string `json:"avatar"`
	Identity int    `json:"identity"`
	Account  string `json:"account"`
	Status   string `json:"status"`
	Remarks  string `json:"remarks"`
}

func UserInfo(userId int) (*UserI, error) {
	var info UserI
	er := DB.Table("media_users u").Select("u.id,ui.account,u.identity,ui.avatar,u.status,u.remarks").Joins("left join media_user_info ui on ui.media_user_id=u.id").Where("u.id=?", userId).Scan(&info).Error
	if er != nil {
		return nil, er
	}
	return &info, er
}

func UpdateUserInfo(userId int, data map[string]interface{}) error {
	return DB.Table("media_user_info").Where("media_user_id=?", userId).Update(data).Error
}

func UpdateUserInstitution(userId int, data map[string]interface{}) error {
	return DB.Table("media_user_institution").Where("media_user_id=?", userId).Update(data).Error
}
func GetBindInfo(userId int) (*MediaUserInfo, error) {
	var userInfo MediaUserInfo
	err := DB.Model(&MediaUserInfo{}).Where("media_user_id=?", userId).Find(&userInfo).Error
	if err != nil {
		return nil, err
	}
	return &userInfo, nil
}
